﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.KMThuChi
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        private Boolean kiemtrasua = false;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private string LoaiKhoanMuc = string.Empty;
        private string NoTaiKhoans = string.Empty;
        private string CoTaiKhoans = string.Empty;
        private string DoiTuongs = string.Empty;
        private int ThuChiID = 0;
        private Entities.GetCombobox[] DoiTuong;
        private Entities.GiaTriCombox[] NoTaiKhoan;
        private Entities.GiaTriCombox[] CoTaiKhoan;

        public frmXuLy()
        {
            InitializeComponent();
            Event();
        }
        private Entities.KMThuChi khoanMuc;
        public Entities.KMThuChi KhoanMuc
        {
            get { return khoanMuc; }
            set { khoanMuc = value; }
        }
        private string hanhdong;
        public string Hanhdong
        {
            get { return hanhdong; }
            set { hanhdong = value; }
        }
        // Insert
        public frmXuLy(string hanhdong, Entities.KMThuChi khoanmuc)
        {
            InitializeComponent();
            Event();
            this.hanhdong = hanhdong;
            this.khoanMuc = khoanmuc;
        }
        #endregion

        #region Event
        private void Event()
        {
            btnThoat.MouseDown += new MouseEventHandler(imgThoat_MouseDown);

            cbbLoaiKM.SelectedValueChanged += new EventHandler(cbbLoaiKM_SelectionChanged);
            cbbNoTK.SelectedValueChanged += new EventHandler(cbbCoNo_SelectionChanged);
            cbbCoTK.SelectedValueChanged += new EventHandler(cbbCoTK_SelectionChanged);
            cbbDoiTuong.SelectedValueChanged += new EventHandler(cbbDoiTuong_SelectionChanged);
        }

        private void imgThem_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (this.hanhdong == Common.Constants.Insert)
                {
                    this.Them();
                }
                if (this.hanhdong == Common.Constants.Update)
                {
                    this.Sua();
                }
            }
            catch 
            {  }
        }

        private void imgThoat_MouseDown(object sender, MouseEventArgs e)
        {
            this.Thoat();
        }


        private void cbbLoaiKM_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                try
                {
                    cbbNoTK.DataSource = null;
                    cbbNoTK.Items.Clear();
                    cbbCoTK.DataSource = null;
                    cbbCoTK.Items.Clear();
                }
                catch { }
                LoaiKhoanMuc = cbbLoaiKM.SelectedValue.ToString();
                if (LoaiKhoanMuc == "0")
                {
                    this.HienThiNoTaiKhoan(this.NoTaiKhoan);
                    this.HienThiCoTaiKhoan(this.CoTaiKhoan);
                }
                if (LoaiKhoanMuc == "1")
                {
                    this.HienThiNoTaiKhoan(this.CoTaiKhoan);
                    this.HienThiCoTaiKhoan(this.NoTaiKhoan);
                }
            }
            catch 
            {  }
        }
        private void cbbCoNo_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                NoTaiKhoans = cbbNoTK.SelectedValue.ToString();
            }
            catch 
            {  }
        }
        private void cbbCoTK_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                CoTaiKhoans = cbbCoTK.SelectedValue.ToString();
            }
            catch 
            {  }
        }
        private void cbbDoiTuong_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DoiTuongs = cbbDoiTuong.SelectedValue.ToString();
            }
            catch 
            {  }
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Thoat();
                }
                if (e.KeyCode == Keys.F2)
                {
                    if (this.hanhdong == Common.Constants.Insert)
                    {
                        this.Them();
                    }
                    if (this.hanhdong == Common.Constants.Update)
                    {
                        this.Sua();
                    }
                }
            }
            catch
            {}
        }
        #endregion

        #region FormLoad
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Normal;
                if (this.hanhdong == Common.Constants.Insert)
                {
                    txtMaKM.Text = getID();
                }
                try
                {
                    HienThiLoaiKhoanMuc();
                }
                catch { }
                try
                {
                    HienThiDoiTuong();
                }
                catch { }
                try
                {
                    // load NoTK and CoTK
                    this.BindingData();
                }
                catch { }
                try
                {
                    HienThiNoTaiKhoan(this.NoTaiKhoan);
                    HienThiCoTaiKhoan(this.CoTaiKhoan);
                }
                catch { }
                if (this.hanhdong == Common.Constants.Update)
                {
                    XuLyDuLieuSua();
                }
                txtTenKM.Focus();
            }
            catch 
            {  }
        }

        #endregion

        #region Du Lieu Combox
        private void HienThiLoaiKhoanMuc()
        {
            try
            {
                Entities.GetCombobox[] combox = new Entities.GetCombobox[2];
                Entities.GetCombobox row = new Entities.GetCombobox();
                row.TenCotTenHienThi = Common.Constants.GiaTriCacForm.KhoanMucThu;
                row.TenCotKhoaChinh = "0";
                combox[0] = row;
                Entities.GetCombobox row2 = new Entities.GetCombobox();
                row2.TenCotTenHienThi = Common.Constants.GiaTriCacForm.KhoanMucChi;
                row2.TenCotKhoaChinh = "1";
                combox[1] = row2;
                Common.Utilities.XuLyComboBox(cbbLoaiKM, combox, Common.Constants.GiaTriCacForm.CotTenHienThi, Common.Constants.GiaTriCacForm.CotKhoaChinh);
                cbbLoaiKM.SelectedIndex = 0;
            }
            catch 
            {  }
        }

        private void HienThiDoiTuong()
        {
            try
            {
                DoiTuong = new Entities.GetCombobox[3];
                Entities.GetCombobox row = new Entities.GetCombobox();
                row.TenCotTenHienThi = Common.Constants.GiaTriCacForm.NhanVienCD;
                row.TenCotKhoaChinh = Common.Constants.GiaTriCacForm.NhanVienKD;
                DoiTuong[0] = row;
                Entities.GetCombobox row2 = new Entities.GetCombobox();
                row2.TenCotTenHienThi = Common.Constants.GiaTriCacForm.KhachHangCD;
                row2.TenCotKhoaChinh = Common.Constants.GiaTriCacForm.KhachHangKD;
                DoiTuong[1] = row2;
                Entities.GetCombobox row3 = new Entities.GetCombobox();
                row3.TenCotTenHienThi = Common.Constants.GiaTriCacForm.NhaCungCapCD;
                row3.TenCotKhoaChinh = Common.Constants.GiaTriCacForm.NhaCungCapKD;
                DoiTuong[2] = row3;
                Common.Utilities.XuLyComboBox(cbbDoiTuong, DoiTuong,Common.Constants.GiaTriCacForm.CotKhoaChinh, Common.Constants.GiaTriCacForm.CotTenHienThi);
                cbbDoiTuong.SelectedIndex = 0;
            }
            catch 
            {  }
        }

        private void HienThiNoTaiKhoan(Entities.GiaTriCombox[] values)
        {
            try
            {
                if (values.Length > 0)
                {
                    for (int k = 0; k < values.Length; k++)
                    {
                        cbbNoTK.Items.Add(values[k].TenCotKhoaChinh);
                    }
                    cbbNoTK.SelectedIndex = 0;
                }
            }
            catch 
            {  }
        }

        private void BindingData()
        {
            try
            {
                Entities.GiaTriCombox gitri1 = new Entities.GiaTriCombox("CO");
                CoTaiKhoan = Common.StaticVariable.InterfaceObject.SelectTKKT(gitri1);

                Entities.GiaTriCombox gitri2 = new Entities.GiaTriCombox("NO");
                NoTaiKhoan = Common.StaticVariable.InterfaceObject.SelectTKKT(gitri2);
            }
            catch 
            {  }
        }

        private void HienThiCoTaiKhoan(Entities.GiaTriCombox[] values)
        {
            try
            {
                if (values.Length > 0)
                {
                    for (int k = 0; k < values.Length; k++)
                    {
                        cbbCoTK.Items.Add(values[k].TenCotKhoaChinh);
                    }
                    cbbCoTK.SelectedIndex = 0;
                }
            }
            catch 
            {  }
        }
        #endregion

        #region LayID
        private string getID()
        {
            string str = string.Empty;
            try
            {
                Entities.GetID layid = new Entities.GetID();
                layid.TableName = Common.Constants.GiaTriCacForm.KMThuChi;
                Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);

                if (lay.ColumnName == null)
                {
                    str = Common.Constants.GiaTriCacForm.GiaTriKMThuChi;
                }
                else
                {
                    str = Common.Utilities.ProcessID(lay.ColumnName);
                }
            }
            catch
            {
                str = Common.Constants.GiaTriCacForm.GiaTriKMThuChi; 
            }
            return str;
        }
        #endregion

        #region Hien ThiSua
        //cbbCoTK
        private void HienThiCoTK(ComboBox box,string ma)
        {
            try
            {
                for (int k = 0; k < box.Items.Count; k++)
                {
                    string id = box.Items[k].ToString();
                    if (id == ma)
                    {
                        box.SelectedIndex = k;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch 
            {  }
        }
        //cbbNoTK
        private void HienThiNoTK(ComboBox box,string ma)
        {
            try
            {
                for (int k = 0; k < box.Items.Count; k++)
                {
                    string id = box.Items[k].ToString();
                    if (id == ma)
                    {
                        box.SelectedIndex = k;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch 
            {  }
        }
        private void HienThiDoiTuongSua(string ma)
        {
            try
            {
                for (int k = 0; k < cbbDoiTuong.Items.Count; k++)
                {
                    Entities.GetCombobox id = (Entities.GetCombobox)cbbDoiTuong.Items[k];
                    if (id.TenCotKhoaChinh == ma)
                    {
                        cbbDoiTuong.SelectedIndex = k;
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch 
            {  }
        }

        private void HienThiLoaiKMSua(Boolean ma)
        {
            try
            {
                //string kt ="0";
                if (ma == false)
                {
                    cbbLoaiKM.SelectedIndex = 0;
                }
                else
                {
                    cbbLoaiKM.SelectedIndex = 1;
                }
            }
            catch 
            {  }
        }
        #endregion

        #region CheckPerMission
        private Boolean CheckPerMission()
        {
            return true;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.KMThuChi xuly)
        {
            Boolean kt = true;
            try
            {
                if (xuly.MaKhoanMuc == string.Empty)
                {
                    kt = false;
                    txtMaKM.Focus();
                    MessageBox.Show(Common.MSG.MSG13(MaKM.Text), Common.MSG.TieuDe);
                    return kt;
                }
                if (xuly.TenKhoanMuc == string.Empty)
                {
                    kt = false;
                    txtTenKM.Focus();
                    MessageBox.Show(Common.MSG.MSG13(TenKM.Text), Common.MSG.TieuDe);
                    return kt;
                }
                if (xuly.LoaiKM == null)
                {
                    kt = false;
                    cbbLoaiKM.Focus();
                    MessageBox.Show(Common.MSG.MSG13(LoaiKM.Text), Common.MSG.TieuDe);
                    return kt;
                }
                if (xuly.DoiTuong == string.Empty)
                {
                    kt = false;
                    cbbDoiTuong.Focus();
                    MessageBox.Show(Common.MSG.MSG13(DoiTuong1.Text), Common.MSG.TieuDe);
                    return kt;
                }
                if (xuly.NoTK == string.Empty)
                {
                    kt = false;
                    cbbCoTK.Focus();
                    MessageBox.Show(Common.MSG.MSG13(NoTK.Text), Common.MSG.TieuDe);
                    return kt;
                }
                if (xuly.CoTK == string.Empty)
                {
                    kt = false;
                    cbbCoTK.Focus();
                    MessageBox.Show(Common.MSG.MSG13(CoTK.Text), Common.MSG.TieuDe);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }
        #endregion

        #region CheckConflick
        private Boolean CheckConflick(Entities.KMThuChi manggoc, Entities.KMThuChi sosanh)
        {
            Boolean kt = true;
            try
            {
                this.khoanMuc = sosanh;
                if (manggoc.MaKhoanMuc != sosanh.MaKhoanMuc)
                {
                    kt = false;
                    txtMaKM.Text = sosanh.MaKhoanMuc;
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.TenKhoanMuc != sosanh.TenKhoanMuc)
                {
                    kt = false;
                    txtTenKM.Text = sosanh.TenKhoanMuc;
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.LoaiKM != sosanh.LoaiKM)
                {
                    kt = false;
                    HienThiLoaiKMSua(sosanh.LoaiKM);
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.DoiTuong != sosanh.DoiTuong)
                {
                    kt = false;
                    HienThiDoiTuongSua(sosanh.DoiTuong);
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.NoTK != sosanh.NoTK)
                {
                    kt = false;
                    if (this.khoanMuc.LoaiKM == true)
                    {
                        this.HienThiNoTaiKhoan(this.NoTaiKhoan);
                        this.HienThiCoTaiKhoan(this.CoTaiKhoan);
                    }
                    if (this.khoanMuc.LoaiKM == false)
                    {
                        this.HienThiNoTaiKhoan(this.CoTaiKhoan);
                        this.HienThiCoTaiKhoan(this.NoTaiKhoan);
                    }
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.CoTK != sosanh.CoTK)
                {
                    kt = false;
                    if (this.khoanMuc.LoaiKM == true)
                    {
                        this.HienThiNoTaiKhoan(this.NoTaiKhoan);
                        this.HienThiCoTaiKhoan(this.CoTaiKhoan);
                    }
                    if (this.khoanMuc.LoaiKM == false)
                    {
                        this.HienThiNoTaiKhoan(this.CoTaiKhoan);
                        this.HienThiCoTaiKhoan(this.NoTaiKhoan);
                    }
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.GhiChu != sosanh.GhiChu)
                {
                    kt = false;
                    txtGhichu.Text = sosanh.GhiChu;
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
                if (manggoc.Deleted != sosanh.Deleted)
                {
                    kt = false;
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe);
                    return kt;
                }
            }
            catch 
            {  kt = false; }
            return kt;
        }

        private Entities.KMThuChi MangSoSanh()
        {
            Entities.KMThuChi sosanh = null;
            try
            {
                Entities.KMThuChi gitri = new Entities.KMThuChi(this.khoanMuc.MaKhoanMuc);
                Entities.KMThuChi[] km = Common.StaticVariable.InterfaceObject.SelectKMThuChi(gitri);

                sosanh = km[0];
            }
            catch 
            {  sosanh = null; }
            return sosanh;
        }
        #endregion

        #region Xu Ly
        private void XuLyDuLieuSua()
        {
            try
            {
                ThuChiID = this.khoanMuc.ThuChiID;
                txtMaKM.Text = this.khoanMuc.MaKhoanMuc;
                txtTenKM.Text = this.khoanMuc.TenKhoanMuc;
                this.HienThiLoaiKMSua(this.khoanMuc.LoaiKM);
                this.HienThiDoiTuongSua(this.khoanMuc.DoiTuong);
                if (this.khoanMuc.LoaiKM == false)
                {
                    this.HienThiNoTaiKhoan(this.NoTaiKhoan);
                    this.HienThiCoTaiKhoan(this.CoTaiKhoan);
                }
                if (this.khoanMuc.LoaiKM == true)
                {
                    this.HienThiNoTaiKhoan(this.CoTaiKhoan);
                    this.HienThiCoTaiKhoan(this.NoTaiKhoan);
                }
                txtGhichu.Text = this.khoanMuc.GhiChu;
            }
            catch 
            {  }
        }

        private Entities.KMThuChi LayDuLieu(string hanhdong)
        {
            Entities.KMThuChi xuly = new Entities.KMThuChi();
            try
            {
                xuly.HanhDong = hanhdong;
                xuly.ThuChiID = ThuChiID;
                xuly.MaKhoanMuc = txtMaKM.Text;
                xuly.TenKhoanMuc = txtTenKM.Text;
                if (this.LoaiKhoanMuc == "0")
                {
                    xuly.LoaiKM = false;
                }
                else
                {
                    xuly.LoaiKM = true;
                }
                xuly.DoiTuong = DoiTuongs;
                xuly.NoTK = cbbNoTK.Text;
                xuly.CoTK = cbbCoTK.Text;
                xuly.GhiChu = txtGhichu.Text;
                xuly.Deleted = false;
                xuly.TaiKhoan = UtilitiesVariable.User;
            }
            catch 
            {  }
            return xuly;
        }
        private void InsertUpdate(string hanhdong)
        {
            try
            {
                Entities.KMThuChi xuly = LayDuLieu(hanhdong);
                if (Validate(xuly) != true)
                {
                    return;
                }
                int i = Common.StaticVariable.InterfaceObject.InsertUpdateKMThuChi(xuly);

                if (i != 0)
                {
                     
                    if (hanhdong == Common.Constants.Insert)
                    {
                        CheckClose = true;
                        this.Close();
                    }
                    else
                    {
                        CheckClose = false;
                        kiemtrasua = true;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG15(txtMaKM.Text), Common.MSG.TieuDe);
                }
            }
            catch
            { }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                string id = getID();
                if (txtMaKM.Text == id)
                {
                    InsertUpdate(Common.Constants.Insert);
                    
                }
                else
                {
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe); 
                    txtMaKM.Text = id; 
                }
            }
            catch 
            {  }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (CheckPerMission() != true)
                {
                    return;
                }
                if (CheckConflick(this.khoanMuc, MangSoSanh()) != true)
                {
                    return;
                }
                InsertUpdate(Common.Constants.Update);
            }
            catch 
            {  }
        }
        #endregion

        #region Thoat
        private void Window_Closing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (CheckClose == false && kiemtrasua == false)
                {
                    System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, System.Windows.Forms.MessageBoxButtons.YesNo);
                    {
                        if (giatri == System.Windows.Forms.DialogResult.Yes)
                        {
                             
                            e.Cancel = false;
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
                GetData(true);
            }
            catch 
            {  }
        }
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult giatri = System.Windows.Forms.MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (giatri == System.Windows.Forms.DialogResult.Yes)
                    {
                        kiemtrasua = true;
                        CheckClose = false;
                        this.Close();
                    }
                }
            }
            catch 
            {  }
        }
        #endregion

    }
}
